<!DOCTYPE html>
<?php
session_start();
ob_start();
?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="public/style.css" rel="stylesheet" type="text/css"/>  
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <title>quang</title>
    </head>
    <body>
        <div id="header">
            <div id="menu">
                <ul>
                    <li><a href="index.php">HOME</a></li>
                    <li><a href="#">CONTENT</a></li>
                    <li><a href="#">FORUMS</a></li>
                    <li><a href="#">CONTACT</a></li>
                </ul>
            </div>
        </div>       
        <div id="search">
            <div id= "logo"></div>
            <div id= "buttons">
                <input name="search" type="text" value="Search" />
                <input name="submit" type="submit" value="Submit" />
            </div>
        </div>

        <h1 align="center">NEW WORKS</h1>       
        <?php
        if (isset($_GET["logout"]) && $_GET["logout"] == true) {
            unset($_SESSION["id"]);
            header("location:login.php");
        }
        ?>
        <div id="noidung">

            <?php if (isset($_SESSION['id'])): ?>
                <a href="index.php?logout=true"><h3>Logout</h3></a>
            <?php else: ?>
                <a href="login.php"><h3>Login</h3></a>
            <?php endif; ?>
            <br>
            <div class="ajax_show"></div>
            <br>
            <?php
            require_once 'model/db.php';
            require_once 'model/keywords.php';
            require_once 'model/works.php';
            require_once 'model/pagination.php';
            $works = new works();
            $pag = new pagination();
            ?>       
            <br>
            <?php $page = $works->count() / PER_PAGE; ?>

            <?php if (!isset($_GET['page'])): ?>
                <a href='index.php?page=<?php echo $_GET['page'] = 1; ?>'></a>
            <?php endif; ?>
            <div>    
                <a href='index.php?page=1'><< FIRST &nbsp;</a>
                <a href='index.php?page=<?php echo $_GET["page"] - 1 ?>'>< PREV &nbsp;</a>

                <?php
                for ($i = 1; $i <= MAX_PAGE; $i++):
                    ?>
                    <?php if (($_GET["page"] == 1) || ($_GET["page"] == 2) || ($_GET["page"] == 3) || ($_GET["page"] == 4)): ?>            
                        <?php
                        for ($i = 1; $i <= MAX_PAGE; $i++):
                            ?>
                            <a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?>&nbsp;</a>
                            <?php if ($_GET["page"] == $i): ?>
                                <?php $works->start = (($i - 1) * $works->page); ?>
                                <?php $works->page = PER_PAGE; ?>                       
                            <?php endif; ?>
                        <?php endfor; ?>

                    <?php elseif (($_GET["page"] == $page) || ($_GET["page"] == $page - 1) || ($_GET["page"] == $page - 2) || ($_GET["page"] == $page - 3) || ($_GET["page"] == $page - 4)): ?>        
                        <?php
                        for ($i = $page - MAX_PAGE; $i <= $page; $i++):
                            ?>
                            <a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?>&nbsp;</a>
                            <?php if ($_GET["page"] == $i): ?>
                                <?php $works->start = (($i - 1) * $works->page); ?>
                                <?php $works->page = PER_PAGE; ?>
                            <?php endif; ?>
                        <?php endfor; ?>

                    <?php else : ?>
                        <?php
                        for ($i = $_GET["page"] - 4; $i <= $_GET["page"] + 5; $i++):
                            ?>
                            <a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?>&nbsp;</a>
                            <?php if ($_GET["page"] == $i): ?>
                                <?php $works->start = (($i - 1) * $works->page); ?>
                                <?php $works->page = PER_PAGE; ?>
                            <?php endif; ?>
                        <?php endfor; ?>
                    <?php endif; ?> 
                <?php endfor; ?>

                <a href='index.php?page=<?php echo $_GET["page"] + 1 ?>'> NEXT >&nbsp;</a>                
                <a href='index.php?page=<?php echo $page; ?>'> LAST >> </a>            
            </div>
            <?php $list_of_works = $works->select(); ?>
            <br><br>
            <table border="1px">
                <tr>
                    <td>id </td>
                    <td>name </td>
                    <td>url </td>
                    <td>image </td>  
                    <td>DK </td>
                </tr>
                <?php foreach ($list_of_works as $list1): ?>
                    <tr>
                        <td><?php echo $list1["id"]; ?></td>
                        <td><?php echo $list1["name"]; ?></td>
                        <td><?php echo substr($list1["description"], 0, 70); ?></td>
                        <td><img src="<?php echo $list1['image']; ?>" width="120px"></td>
                        <?php if (isset($_SESSION['id']) && $_SESSION['id'] == 0) : ?>
                            <td>
                                <a href="upload.php?id=<?php echo $list1['id']; ?>"><?php echo "EDIT" ?></a><br>
                                <a href="#" class="ajax_view" id_works="<?php echo $list1['id']; ?>"><?php echo "VIEW" ?></a><br>
                                <a href='#'><?php echo "DELETE" ?></a>
                            </td>  
                        <?php else: ?>
                            <td>
                                <a href="#" class="ajax_view" id_works="<?php echo $list1['id']; ?>"><?php echo "VIEW" ?></a><br>
                            </td>
                        <?php endif; ?>
                    </tr>
                <?php endforeach; ?>
            </table>
            <br>           
            <br>
        </div>
        <br>       
        <script type="text/javascript" >
            $(document).ready(function() {
                $(".ajax_view").click(function() {
                    id_works = $(this).attr('id_works');
                    url = "ajax.php?id_works=" + id_works;
                    $.ajax({
                        url: url
                    }).done(function(data) {
                        $('.ajax_show').html(data);
                    });
                });
            });
        </script>
    </body>
</html>
<?php ob_end_flush(); ?>